import logging
from logging.handlers import RotatingFileHandler


def loggingInit():
    # # 将日志记录到文件
    # logging.basicConfig(
    #     level=logging.INFO,
    #     format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    #     datefmt='%Y-%m-%d %H:%M:%S',
    #     filename='./log/app.log',
    #     filemode='a'  # 'a' 表示追加模式，'w' 表示覆盖模式
    # )
    # 定义日志文件路径和大小限制
    log_file = "./log/app.log"  # 日志文件路径
    max_log_size = 1 * 1024 * 1024  # 日志文件大小上限为 1 MB
    backup_count = 5  # 最多保留 5 个备份文件

    # 创建一个 RotatingFileHandler
    rotating_handler = RotatingFileHandler(
        log_file,
        maxBytes=max_log_size,
        backupCount=backup_count
    )

    # 设置日志格式
    log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    formatter = logging.Formatter(log_format)
    rotating_handler.setFormatter(formatter)

    # 使用 basicConfig 配置日志
    logging.basicConfig(
        level=logging.DEBUG,  # 设置全局日志级别
        handlers=[rotating_handler],  # 将 RotatingFileHandler 添加到 handlers 参数中
    )


def addLog(module: str, level: str, msg: str):
    logger = logging.getLogger(module)

    if level == 'info':
        logger.info(msg)
    elif level == 'warn':
        logger.warning(msg)
    elif level == 'error':
        logger.error(msg)
    else:
        logger.info(msg)



